package com.business_clean.ui.activity.classes;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;

import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.callback.OnDialogClockDateSelect;
import com.business_clean.app.callback.OnDialogRestDayListener;
import com.business_clean.app.config.ConstantMMVK;
import com.business_clean.app.uitl.MMKVHelper;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.app.weight.dialog.CustomClockDate;
import com.business_clean.app.weight.dialog.CustomRestDayWheelPopup;
import com.business_clean.data.mode.classes.ClassesDetailEntity;
import com.business_clean.data.mode.classes.ClassesListEntity;
import com.business_clean.data.mode.init.BaseIDNameEntity;
import com.business_clean.data.mode.init.ClassTime;
import com.business_clean.data.mode.init.OutTime;
import com.business_clean.databinding.ActivityAddClassesBinding;
import com.business_clean.viewmodel.request.ClassexViewModel;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddClassesActivity extends BaseActivity<ClassexViewModel, ActivityAddClassesBinding> {

    private ClassTime classTime;
    private OutTime outTime;

    private String startClockTime;
    private String endClockTime;
    private int isStartClockNowDay;
    private int isEndClockNowDay;


    private String startRestTime;
    private String endRestTime;
    private int isStartRestNowDay;
    private int isEndRestNowDay;

    private String in_class_start_type;
    private String in_class_start_type_name;
    private String in_class_end_type;
    private String in_class_end_type_name;


    private String out_class_start_type;
    private String out_class_start_type_name;
    private String out_class_end_type;
    private String out_class_end_type_name;

    private String uuid;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (getIntent() != null && getIntent().getExtras() != null) {
            uuid = getIntent().getExtras().getString("uuid");
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("project_uuid", "" + App.appViewModelInstance.getProjectInfo().getValue().getUuid());
            hashMap.put("uuid", "" + uuid);
            mViewModel.requestClassesDetail(hashMap);
            mDatabind.tabbar.setTitle("编辑班次");
        }

        //上卡的时间
        String inClassJson = MMKVHelper.getString(ConstantMMVK.IN_CLASS_TIME_LIST);
        classTime = new Gson().fromJson(inClassJson, ClassTime.class);
        //上下班打卡的时间
        String outClassJson = MMKVHelper.getString(ConstantMMVK.OUT_CLASS_TIME_LIST);
        outTime = new Gson().fromJson(outClassJson, OutTime.class);

        //设置默认值
        if (TextUtils.isEmpty(uuid)) {
            if (classTime != null) {
                if (classTime.getBefore_list().size() > 3) {
                    in_class_start_type = classTime.getBefore_list().get(3).getId();
                    in_class_start_type_name = classTime.getBefore_list().get(3).getName();
                }
                if (classTime.getAfter_list().size() > 0) {
                    in_class_end_type = classTime.getAfter_list().get(0).getId();
                    in_class_end_type_name = classTime.getAfter_list().get(0).getName();
                }
                if (!TextUtils.isEmpty(in_class_start_type_name) && !TextUtils.isEmpty(in_class_end_type_name)) {
                    mDatabind.withWorkClock.setTvRightText(in_class_start_type_name + "-" + in_class_end_type_name);
                }
            }

            if (outTime != null) {
                if (outTime.getBefore_list().size() > 0) {
                    out_class_start_type = outTime.getBefore_list().get(0).getId();
                    out_class_start_type_name = outTime.getBefore_list().get(0).getName();
                }
                if (outTime.getAfter_list().size() > 3) {
                    out_class_end_type = outTime.getAfter_list().get(3).getId();
                    out_class_end_type_name = outTime.getAfter_list().get(3).getName();
                }
                if (!TextUtils.isEmpty(out_class_start_type_name) && !TextUtils.isEmpty(out_class_end_type_name)) {
                    mDatabind.withWorkOffWork.setTvRightText(out_class_start_type_name + "-" + out_class_end_type_name);
                }
            }
        }

        mDatabind.bottom.butBase.setText("保存");
    }

    @Override
    public void onBindViewClick() {
        //上下班时间段
        mDatabind.withGoToWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (classTime != null && classTime.getAfter_list() != null && classTime.getBefore_list() != null) {
                    new XPopup.Builder(AddClassesActivity.this)
                            .asCustom(new CustomRestDayWheelPopup(AddClassesActivity.this, false, isStartClockNowDay, startClockTime, isEndClockNowDay, endClockTime, new OnDialogRestDayListener() {
                                @Override
                                public void selected(int isStartNowDay, String startTime, int isEndNowDay, String endTime) {
                                    startClockTime = startTime;
                                    endClockTime = endTime;
                                    isStartClockNowDay = isStartNowDay;
                                    isEndClockNowDay = isEndNowDay;
                                    StringBuilder stringBuilder = new StringBuilder();
                                    if (isStartClockNowDay == 1) {//说明是次日
                                        stringBuilder.append("次");
                                    }
                                    stringBuilder.append(startTime).append("-");
                                    if (isEndClockNowDay == 1) {//说明是次日
                                        stringBuilder.append("次");
                                    }
                                    stringBuilder.append(endTime);
                                    mDatabind.withGoToWork.setTvRightText(stringBuilder.toString());
                                }
                            })).show();
                }
            }
        });


        //休息时段
        mDatabind.withRest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (outTime != null && outTime.getAfter_list() != null && outTime.getBefore_list() != null) {
                    new XPopup.Builder(AddClassesActivity.this)
                            .asCustom(new CustomRestDayWheelPopup(AddClassesActivity.this, true, isStartRestNowDay, startRestTime, isEndRestNowDay, endRestTime, new OnDialogRestDayListener() {
                                @Override
                                public void selected(int isStartNowDay, String startTime, int isEndNowDay, String endTime) {
                                    startRestTime = startTime;
                                    endRestTime = endTime;
                                    isStartRestNowDay = isStartNowDay;
                                    isEndRestNowDay = isEndNowDay;

                                    StringBuilder stringBuilder = new StringBuilder();
                                    if (isStartRestNowDay == 1) {//说明是次日
                                        stringBuilder.append("次");
                                    }
                                    stringBuilder.append(startTime).append("-");
                                    if (isEndRestNowDay == 1) {//说明是次日
                                        stringBuilder.append("次");
                                    }
                                    stringBuilder.append(endTime);

                                    mDatabind.tvRestText.setText(stringBuilder.toString());
                                    mDatabind.tvRestText.setTextColor(ContextCompat.getColor(AddClassesActivity.this, R.color.base_primary_text_title));
                                    mDatabind.tvClean.setVisibility(View.VISIBLE);
                                }
                            })).show();
                }
            }
        });


        //上班时间段
        mDatabind.withWorkClock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (classTime != null && classTime.getAfter_list() != null && classTime.getBefore_list() != null) {
                    new XPopup.Builder(AddClassesActivity.this)
                            .asCustom(new CustomClockDate(AddClassesActivity.this, getArrayListString(classTime.getBefore_list()),
                                    getArrayListString(classTime.getAfter_list()), in_class_start_type_name, in_class_end_type_name, new OnDialogClockDateSelect() {
                                @Override
                                public void onSelected(int select1, int select2) {
                                    in_class_start_type = classTime.getBefore_list().get(select1).getId();
                                    in_class_end_type = classTime.getAfter_list().get(select2).getId();
                                    in_class_start_type_name = classTime.getBefore_list().get(select1).getName();
                                    in_class_end_type_name = classTime.getAfter_list().get(select2).getName();
                                    mDatabind.withWorkClock.setTvRightText(in_class_start_type_name + "-" + in_class_end_type_name);
                                }
                            }
                            )).show();
                }
            }
        });

        //下班时间段
        mDatabind.withWorkOffWork.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (outTime != null && outTime.getAfter_list() != null && outTime.getBefore_list() != null) {
                    if (outTime != null && outTime.getAfter_list() != null && outTime.getBefore_list() != null) {
                        new XPopup.Builder(AddClassesActivity.this)
                                .asCustom(new CustomClockDate(AddClassesActivity.this, getArrayListString(outTime.getBefore_list()),
                                        getArrayListString(outTime.getAfter_list()), out_class_start_type_name, out_class_end_type_name, new OnDialogClockDateSelect() {
                                    @Override
                                    public void onSelected(int select1, int select2) {
                                        out_class_start_type = outTime.getBefore_list().get(select1).getId();
                                        out_class_end_type = outTime.getAfter_list().get(select2).getId();
                                        out_class_start_type_name = outTime.getBefore_list().get(select1).getName();
                                        out_class_end_type_name = outTime.getAfter_list().get(select2).getName();
                                        mDatabind.withWorkOffWork.setTvRightText(out_class_start_type_name + "-" + out_class_end_type_name);
                                    }
                                }
                                )).show();
                    }
                }
            }
        });

        //清楚休息时间
        mDatabind.tvClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatabind.tvRestText.setText("请选择");
                mDatabind.tvRestText.setTextColor(ContextCompat.getColor(AddClassesActivity.this, R.color.base_primary_text_hint));
                startRestTime = "";
                endRestTime = "";
                mDatabind.tvClean.setVisibility(View.GONE);
            }
        });

        //保存
        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mDatabind.withName.getInputText())) {
                    ToastUtil.show("请输入班次名称");
                    return;
                }
                if (TextUtils.isEmpty(mDatabind.withGoToWork.getTvRight())) {
                    ToastUtil.show("请选择上下班时间段");
                    return;
                }
                if (TextUtils.isEmpty(mDatabind.withWorkClock.getTvRight())) {
                    ToastUtil.show("请选择上班打卡");
                    return;
                }
                if (TextUtils.isEmpty(mDatabind.withWorkOffWork.getTvRight())) {
                    ToastUtil.show("请选择下班打卡");
                    return;
                }
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("project_uuid", "" + App.appViewModelInstance.getUserInfo().getValue().getProject().getUuid());//项目UUID
                if (!TextUtils.isEmpty(uuid)) {
                    hashMap.put("uuid", "" + uuid);//班次uuid 编辑时必天
                }
                hashMap.put("class_name", "" + mDatabind.withName.getInputText());//班次名称
                //开始时间
                hashMap.put("work_start_time", "" + startClockTime);//上班开始时间
                hashMap.put("work_end_time", "" + endClockTime);//上班结束时间
                hashMap.put("work_is_today", "" + (isEndClockNowDay + 1));//上班结束时间是否今日 1是2否 默认是
                //休息时间
                hashMap.put("rest_start_time_is_today", "" + (isStartRestNowDay + 1));//休息开始时间是否今日 1是2否 有休息时段则必传
                hashMap.put("rest_end_time_is_today", "" + (isEndRestNowDay + 1));//休息结束时间是否今日 1是2否 有休息时段则必传
                if (!TextUtils.isEmpty(startRestTime) && !TextUtils.isEmpty(endRestTime)) {
                    hashMap.put("rest_start_time", "" + startRestTime);//休息开始时间
                    hashMap.put("rest_end_time", "" + endRestTime);//休息结束时间
                }
                if (!TextUtils.isEmpty(in_class_start_type)) {
                    hashMap.put("in_class_start_type", "" + in_class_start_type);
                }
                if (!TextUtils.isEmpty(in_class_end_type)) {
                    hashMap.put("in_class_end_type", "" + in_class_end_type);
                }
                if (!TextUtils.isEmpty(out_class_start_type)) {
                    hashMap.put("out_class_start_type", "" + out_class_start_type);
                }
                if (!TextUtils.isEmpty(out_class_end_type)) {
                    hashMap.put("out_class_end_type", "" + out_class_end_type);
                }
                mViewModel.requestSaveContact(hashMap);
            }
        });
    }

    @Override
    public void initObserver() {
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getSaveClassesState().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                App.appViewModelInstance.getRefreshClassesManagerList().setValue(true);
                finish();
            }
        });

        mViewModel.getClassesEntity().observe(this, new Observer<ClassesDetailEntity>() {
            @Override
            public void onChanged(ClassesDetailEntity classesListEntity) {
                if (classesListEntity != null) {
                    fillData(classesListEntity);
                }
            }
        });
    }

    private void fillData(ClassesDetailEntity classesListEntity) {
        if (classesListEntity != null) {
            mDatabind.withName.setInputText(classesListEntity.getClass_name());


            in_class_start_type = classesListEntity.getIn_class_start_type();
            in_class_end_type = classesListEntity.getIn_class_end_type();
            out_class_start_type = classesListEntity.getOut_class_start_type();
            out_class_end_type = classesListEntity.getOut_class_end_type();

            startClockTime = classesListEntity.getWork_start_time();
            endClockTime = classesListEntity.getWork_end_time();
            isStartClockNowDay = Integer.parseInt(classesListEntity.getWork_is_today()) - 1;
            isEndClockNowDay = Integer.parseInt(classesListEntity.getWork_is_today()) - 1;
            mDatabind.withGoToWork.setTvRightText(startClockTime + "-" + endClockTime);


            startRestTime = classesListEntity.getRest_start_time();
            endRestTime = classesListEntity.getRest_end_time();
            isStartRestNowDay = Integer.parseInt(classesListEntity.getRest_start_time_is_today()) - 1;
            isEndRestNowDay = Integer.parseInt(classesListEntity.getRest_end_time_is_today()) - 1;
            if (!TextUtils.isEmpty(classesListEntity.getRest_start_time()) && !TextUtils.isEmpty(classesListEntity.getRest_end_time())) {
                mDatabind.tvRestText.setText(classesListEntity.getRest_start_time() + "-" + classesListEntity.getRest_end_time());
                mDatabind.tvRestText.setTextColor(ContextCompat.getColor(AddClassesActivity.this, R.color.base_primary_text_title));
                mDatabind.tvClean.setVisibility(View.VISIBLE);
            }


            if (classTime != null) {
                for (int i = 0; i < classTime.getBefore_list().size(); i++) {
                    if (in_class_start_type.equals(classTime.getBefore_list().get(i).getId())) {
                        in_class_start_type_name = classTime.getBefore_list().get(i).getName();
//                        in_class_start_type = classTime.getBefore_list().get(i).getId();
                        break;
                    }
                }
                for (int i = 0; i < classTime.getAfter_list().size(); i++) {
                    if (in_class_end_type.equals(classTime.getAfter_list().get(i).getId())) {
//                        in_class_end_type = classTime.getAfter_list().get(i).getId();
                        in_class_end_type_name = classTime.getAfter_list().get(i).getName();
                        break;
                    }
                }
                mDatabind.withWorkClock.setTvRightText(in_class_start_type_name + "-" + in_class_end_type_name);
            }

            if (outTime != null) {
                for (int i = 0; i < outTime.getBefore_list().size(); i++) {
                    if (out_class_start_type.equals(outTime.getBefore_list().get(i).getId())) {
                        out_class_start_type_name = outTime.getBefore_list().get(i).getName();
                        break;
                    }
                }
                for (int i = 0; i < outTime.getAfter_list().size(); i++) {
                    if (out_class_end_type.equals(outTime.getAfter_list().get(i).getId())) {
                        out_class_end_type_name = outTime.getAfter_list().get(i).getName();
                        break;
                    }
                }
                mDatabind.withWorkOffWork.setTvRightText(out_class_start_type_name + "-" + out_class_end_type_name);
            }
        }
    }


    private List<String> getArrayListString(List<BaseIDNameEntity> list) {
        List<String> names = new ArrayList<>();
        for (BaseIDNameEntity item : list) {
            names.add(item.getName());
        }
        return names;
    }
}